#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

create_dir()
{
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p $path; else path=$path`date "+%y%m%d%H%M%S"` && mkdir -p $path; fi
    echo $path
}

buildpath=$(create_dir $1)
installpath=$(create_dir $2)
#buildpath=$1
#installpath=$2

yum install -y m4 environment-modules systemd-devel cmake gcc gcc-c++ python python-devel 
#source /etc/profile
module purge
set -e
dep_dir="dependency"

bash $dep_dir/bisheng-compiler-2.4.0.sh $buildpath $installpath
module load $installpath/bisheng-compiler-2.4.0/bisheng_modulefiles

bash $dep_dir/hmpi-1.2.0.sh $buildpath $installpath
module load $installpath/hmpi-1.2.0/hmpi_modulefiles

bash $dep_dir/lapack-3.10.1.sh $buildpath $installpath
module load $installpath/lapack-3.10.1/lapack_modulefiles

bash $dep_dir/libxc-4.3.4.sh $buildpath $installpath
module load $installpath/libxc-4.3.4/libxc_modulefiles

bash $dep_dir/fftw-3.3.8.sh $buildpath $installpath
module load $installpath/fftw-3.3.8/fftw_modulefiles

bash $dep_dir/libvdwxc-0.4.0.sh $buildpath $installpath
module load $installpath/libvdwxc-0.4.0/libvdwxc_modulefiles

bash $dep_dir/gsl-2.5.sh $buildpath $installpath
module load $installpath/gsl-2.5/gsl_modulefiles

bash $dep_dir/octopus-11.4.sh $buildpath $installpath
module load $installpath/octopus-11.4/octopus_modulefiles
